package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctTrainingRecordsBo;
import com.rzdata.poct.system.domain.business.po.PoctTrainingRecords;
import com.rzdata.poct.system.domain.business.vo.PoctTrainingRecordsVo;
import com.rzdata.poct.system.mapper.PoctTrainingRecordsMapper;
import com.rzdata.poct.system.service.IPoctTrainingRecordsService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctTrainingRecordsServiceImpl extends ServicePlusImpl<PoctTrainingRecordsMapper, PoctTrainingRecords, PoctTrainingRecordsVo> implements IPoctTrainingRecordsService {

    @Override
    public PoctTrainingRecordsVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctTrainingRecordsVo> queryPageList(PoctTrainingRecordsBo bo) {
        PagePlus<PoctTrainingRecords, PoctTrainingRecordsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public IPage<PoctTrainingRecordsVo> listPage(PoctTrainingRecordsBo bo) {
        Page<PoctTrainingRecordsVo> page = new Page<>(bo.getPageNum(), bo.getPageSize());
        return this.baseMapper.listPage(page, bo);
    }

    @Override
    public List<PoctTrainingRecordsVo> queryList(PoctTrainingRecordsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctTrainingRecords> buildQueryWrapper(PoctTrainingRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctTrainingRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTrainingNo()), PoctTrainingRecords::getTrainingNo, bo.getTrainingNo());
        lqw.like(StringUtils.isNotBlank(bo.getTrainingName()), PoctTrainingRecords::getTrainingName, bo.getTrainingName());
        lqw.eq(bo.getProjectId() != null, PoctTrainingRecords::getProjectId, bo.getProjectId());
        lqw.eq(bo.getFileType() != null, PoctTrainingRecords::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getLocal()), PoctTrainingRecords::getLocal, bo.getLocal());
        lqw.eq(StringUtils.isNotBlank(bo.getLecturer()), PoctTrainingRecords::getLecturer, bo.getLecturer());
        lqw.eq(bo.getStartTime() != null, PoctTrainingRecords::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, PoctTrainingRecords::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTrainingDescribe()), PoctTrainingRecords::getTrainingDescribe, bo.getTrainingDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getFileId()), PoctTrainingRecords::getFileId, bo.getFileId());
        lqw.eq(bo.getType() != null, PoctTrainingRecords::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStudent()), PoctTrainingRecords::getStudent, bo.getStudent());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctTrainingRecordsBo bo) {
        PoctTrainingRecords add = BeanUtil.toBean(bo, PoctTrainingRecords.class);
        add.setCreateTime(new Date());
        if (ObjectUtil.isNotEmpty(bo.getStartTime()) && ObjectUtil.isNotEmpty(bo.getEndTime())) {
            Long time = (bo.getEndTime().getTime() - bo.getStartTime().getTime()) / 6000;
            add.setElapsedTime(time);
        }
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctTrainingRecordsBo bo) {
        PoctTrainingRecords update = BeanUtil.toBean(bo, PoctTrainingRecords.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctTrainingRecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
